﻿using System;
using Microsoft.SharePoint;

namespace QueryDemo
{
	/// <summary>
	/// Stores essential list properties for display in the UI and use of the query.
	/// Aimed to prevent querying the database for SPLists.
	/// </summary>
	public class ListMetadata
	{
		public string Title { get; set; }

		public Guid UniqueId { get; set; }

		public int ServerTemplate { get; set; }

		private const string PropertySeparator = ";";

		/// <summary>
		/// Parses delimiter-separated properties and sets within this object.
		/// </summary>
		/// <param name="combinedProperties">Properties given by PropertySeparator.</param>
		public ListMetadata(string combinedProperties)
		{
			string[] props = combinedProperties.Split(new []{ PropertySeparator }, StringSplitOptions.None);
			UniqueId = new Guid(props[0]);
			ServerTemplate = Int32.Parse(props[1]);
		}

		/// <summary>
		/// Parses SPList properties and sets within this object.
		/// </summary>
		/// <param name="list">SPList object to parse.</param>
		public ListMetadata(SPList list)
		{
			Title = list.Title;
			UniqueId = list.ID;
			// TODO this.ServerTemplate = list
		}

		/// <summary>
		/// Mashes together the non-title properties so they can be associated with a ListItem.Value property.
		/// </summary>
		/// <returns>Combined properties.</returns>
		public string GetCombinedProperties()
		{
			return UniqueId + PropertySeparator + ServerTemplate;
		}
	}
}
